package ecnu.liwaTest.dd.pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import java.util.ArrayList;
import java.util.List;

import static ecnu.liwaTest.dd.DriverSingleton.getChromeDriver;
import static ecnu.liwaTest.dd.DriverSingleton.waitForWindow;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;

public class SchedulePage extends BasePage{

    WebDriver driver = getChromeDriver();

    public SchedulePage goSchedulePage() {
        driver.findElement(By.cssSelector(".el-menu-item:nth-child(3)")).click();
        waitForWindow(1000);
        assertThat(driver.getTitle(), is("排班表"));
        return this;
    }

    public SchedulePage checkDdScheduleNum() {
        driver.findElement(By.cssSelector(".dateItem:nth-child(4) > .dayMarking")).click();
        waitForWindow(1000);
        driver.findElement(By.cssSelector(".el-col-12:nth-child(1)")).click();
        waitForWindow(2000);
        String num = driver.findElement(By.cssSelector(".activeDate > .watcher:nth-child(3) > span")).getText();

        List<WebElement> elements = driver.findElements(By.cssSelector(".textOverflow"));
        assertThat(Integer.parseInt(num), is(elements.size()));
        driver.findElement(By.cssSelector(".el-col-12:nth-child(1)")).click();
        return this;
    }

    public SchedulePage checkDdSchedule(String name) {
        driver.findElement(By.cssSelector(".dateItem:nth-child(4) > .dayMarking")).click();
        driver.findElement(By.cssSelector(".el-col-12:nth-child(1)")).click();

        waitForWindow(2000);
        List<WebElement> elementList = driver.findElements(By.cssSelector(".textOverflow"));
        List<String> stringList = new ArrayList<>();
        for(WebElement element:elementList) {
            stringList.add(element.getText());
        }
        assertThat(stringList, hasItem(containsString(name)));
        driver.findElement(By.cssSelector(".el-col-12:nth-child(1)")).click();
        return this;
    }

    public SchedulePage checkDdScheduleNull(String message) {
        driver.findElement(By.cssSelector(".dateItem:nth-child(4) > .dayMarking")).click();
        driver.findElement(By.cssSelector(".el-col-12:nth-child(1)")).click();

        waitForWindow(2000);
        String str = driver.findElement(By.cssSelector(".userList > .el-row")).getText();
        assertThat(str, is(message));
        driver.findElement(By.cssSelector(".el-col-12:nth-child(1)")).click();
        return this;
    }

    public SchedulePage addDd(String name) {
        driver.findElement(By.cssSelector(".dateItem:nth-child(4) > .dayMarking")).click();
        driver.findElement(By.cssSelector(".el-col-12:nth-child(1)")).click();
        driver.findElement(By.cssSelector(".el-row:nth-child(3)")).click();
        driver.findElement(By.cssSelector(".el-select__caret")).click();
        waitForWindow(1000);

        List<WebElement> elementList = driver.findElements(By.className("el-select-dropdown__item"));
        for(WebElement element:elementList) {
            if(element.getText().contains(name)) {
                //driver.findElement(By.cssSelector(".hover > span")).click();
                element.click();
                break;
            }
        }

        driver.findElement(By.className("modalTitle")).click();
        waitForWindow(1000);
        driver.findElement(By.cssSelector(".el-button--default:nth-child(2)")).click();
        driver.findElement(By.cssSelector(".el-col-12:nth-child(1)")).click();
        return this;
    }

    public SchedulePage removeDd(String name) {
        //12/4
        driver.findElement(By.cssSelector(".dateItem:nth-child(4) > .dayMarking")).click();
        driver.findElement(By.cssSelector(".el-col-12:nth-child(1)")).click();

        waitForWindow(1000);
        WebElement elements = driver.findElement(By.cssSelector(".userList"));
        List<WebElement> elementList = elements.findElements(By.cssSelector(".el-row"));
        //find the user and click remove
        for(WebElement element:elementList) {
            waitForWindow(1000);
            if(element.getText().contains(name)) {
                element.findElement(By.cssSelector(".el-col:nth-child(3)")).click();
                waitForWindow(1000);
                driver.findElement(By.cssSelector(".el-button--small:nth-child(2) > span")).click();
                break;
            }
        }

        //check name is not in the user list
        elements = driver.findElement(By.cssSelector(".userList"));
        waitForWindow(1000);
        elementList = elements.findElements(By.cssSelector(".el-row"));
        waitForWindow(1000);
        for(WebElement element:elementList) {
            waitForWindow(1000);
            assertThat(element.getText(), not(containsString(name)));
        }
        return this;
    }

    public SchedulePage verifyDdInList(String name) {
        driver.findElement(By.cssSelector(".el-col-12:nth-child(1)")).click();
        return verifyInList(name);
    }

    //咨询师

    public SchedulePage checkZxsScheduleNum() {
        driver.findElement(By.cssSelector(".dateItem:nth-child(4) > .dayMarking")).click();
        //driver.findElement(By.cssSelector(".el-col-12:nth-child(1)")).click();

        waitForWindow(2000);
        String num = driver.findElement(By.cssSelector(".activeDate > .watcher:nth-child(2) > span")).getText();

        List<WebElement> elements = driver.findElements(By.cssSelector(".textOverflow"));
        assertThat(Integer.parseInt(num), is(elements.size()));
        return this;
    }

    public SchedulePage checkZxsSchedule(String name) {
        driver.findElement(By.cssSelector(".dateItem:nth-child(4) > .dayMarking")).click();
        //driver.findElement(By.cssSelector(".el-col-12:nth-child(1)")).click();

        waitForWindow(2000);
        List<WebElement> elementList = driver.findElements(By.cssSelector(".textOverflow"));
        List<String> stringList = new ArrayList<>();
        for(WebElement element:elementList) {
            stringList.add(element.getText());
        }
        assertThat(stringList, hasItem(containsString(name)));

        return this;
    }

    public SchedulePage checkZxsScheduleNull(String message) {
        driver.findElement(By.cssSelector(".dateItem:nth-child(4) > .dayMarking")).click();
        //driver.findElement(By.cssSelector(".el-col-12:nth-child(2)")).click();

        waitForWindow(2000);
        String str = driver.findElement(By.cssSelector(".userList > .el-row")).getText();
        assertThat(str, is(message));
        return this;
    }

    public SchedulePage addZxs(String name) {
        driver.findElement(By.cssSelector(".dateItem:nth-child(4) > .dayMarking")).click();
        driver.findElement(By.cssSelector(".el-row:nth-child(3)")).click();
        driver.findElement(By.cssSelector(".el-select__caret")).click();
        waitForWindow(1000);

        List<WebElement> elementList = driver.findElements(By.className("el-select-dropdown__item"));
        for(WebElement element:elementList) {
            if(element.getText().contains(name)) {
                //driver.findElement(By.cssSelector(".hover > span")).click();
                element.click();
                break;
            }
        }

        driver.findElement(By.className("modalTitle")).click();
        waitForWindow(1000);
        driver.findElement(By.cssSelector(".el-button--default:nth-child(2)")).click();

        return this;
    }

    public SchedulePage removeZxs(String name) {
        //12/4
        driver.findElement(By.cssSelector(".dateItem:nth-child(4) > .dayMarking")).click();

        waitForWindow(1000);
        WebElement elements = driver.findElement(By.cssSelector(".userList"));
        List<WebElement> elementList = elements.findElements(By.cssSelector(".el-row"));
        //find the user and click remove
        for(WebElement element:elementList) {
            waitForWindow(1000);
            if(element.getText().contains(name)) {
                element.findElement(By.cssSelector(".el-col:nth-child(3)")).click();
                waitForWindow(1000);
                driver.findElement(By.cssSelector(".el-button--small:nth-child(2) > span")).click();
                break;
            }
        }

        //check name is not in the user list
        elements = driver.findElement(By.cssSelector(".userList"));
        waitForWindow(1000);
        elementList = elements.findElements(By.cssSelector(".el-row"));
        waitForWindow(1000);
        for(WebElement element:elementList) {
            waitForWindow(1000);
            assertThat(element.getText(), not(containsString(name)));
        }
        return this;
    }

    public SchedulePage verifyInList(String name) {

        //get the user list
        WebElement elements = driver.findElement(By.cssSelector(".userList"));
        waitForWindow(1000);
        List<WebElement> elementList = elements.findElements(By.cssSelector(".el-row"));
        waitForWindow(1000);

        //store name in a list
        List<String> stringList = new ArrayList<>();
        for(WebElement element:elementList) {
            stringList.add(element.getText());
        }
        //asert that
        assertThat(stringList, hasItem(containsString(name)));
        return this;
    }

}
